// page p0005 controller
var p0005Controller = (function($) {

    var key = 'p0005';
    var selector = '#' + key;
    var page = 'pages/' + key + '.html';
    var tryCount = 0;

    var draw = function (item) {
        if (!$.esPage.queryParams)
            return;
    };

    var showDialog = function (callback, queryParams) {
        $.esPage.showDialog(selector, {
            callback: callback,
            queryParams: queryParams
        });
        app.afterLoad(selector);
    };

    var obj = {

        init: function (callback) {
            app.loadPage(page, function () {
                $(selector).esPage();
                if (callback)
                    callback();
            });
        },

        refresh: draw,
        showDialog: showDialog,

        btnImportClick: function () {
            var iptFile = $(selector).find('#p0005FormUpload')[0];
            if (iptFile.files.length == 0) {
                alert('请先选择一个文件');
                return;
            }

            var file = iptFile.files[0];
            var reader = new FileReader();

            reader.onload = function(evt) {
                if (FileReader.DONE != reader.readyState)
                    return;
                var data = this.result;
                Storage.import(data);
            };
            reader.readAsText(file);
        },

        btnExportClick: function () {
            var href = 'data:application/octet-stream,' + Storage.export();
            var a = $('<a>');
            a.attr('target', '_blank');
            a.attr('href', href);
            a.attr('download', 'onekey.dat');
            $(selector).append(a);
            a[0].click();
            a.remove();
        },

        btnCancelClick: function () {
            $.esPage.closeDialog(true);
        }
    };

    app.registerPageController(key, obj);

    return obj;

})(jQuery);